package edu.zhku.ttms.utils;

import edu.zhku.ttms.domain.User;
import edu.zhku.ttms.mapper.TrafficPolicemanMapper;

/**
 * @author Feng
 * @since 2021/4/19 13:00
 */

public class LoginSession {

    static TrafficPolicemanMapper trafficPolicemanMapper = MybatisUtil.getMapper(TrafficPolicemanMapper.class);

    private static User loggedUser = null;

    private static Long policeId = null;

    public static void login(User user) {
        loggedUser = user;

        if (user.getRoleId() == 2) {
            policeId = trafficPolicemanMapper.getByCode(user.getLoginName()).getPoliceId();
        }
    }

    public static void logout() {
        loggedUser = null;
    }

    public static boolean hasLogged() {
        return loggedUser != null;
    }

    public static String getLoggedUserName() {
        return loggedUser == null ? null : loggedUser.getLoginName();
    }

    public static int getLoggedUserRole() {
        return loggedUser == null ? -1 : loggedUser.getRoleId();
    }

    public static String getLoggedUserRoleStr() {
        int role = getLoggedUserRole();
        switch (role) {
            case 1:
                return "管理员";
            case 2:
                return "交警";
            case 3:
                return "驾驶员";
            default:
                return "";
        }
    }

    public static String getLoggedInfo() {
        if (hasLogged()) {
            return "用户名：" + getLoggedUserName() + " 角色：" + getLoggedUserRoleStr();
        }
        return "未登录！";
    }

    public static Long getLoggedPoliceId() {
        return policeId;
    }
}
